﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HR60_PanelSoft.View.Base;
using HR60_PanelSoft.Model.BaseModel;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.View.TabForms.ListItems;
using HR60_PanelSoft.View.Controls;

namespace HR60_PanelSoft.View.TabForms
{
    //GC: [g1]修正场景编辑bug。[SceneForm] @s3
    //GC: [g2]场景保存，高级设备会重复bug。[SceneForm]
    //GC: [g3]名称修改不能反射到树[SceneForm]
    //GC: [g4]场景类型，不能正确加载
    //GC: [g5]opt3088
	public partial class SceneForm : BaseForm
	{
        /* [g1.s2]: 将scene对象提升至字段 */
        private Scene _cur_scene = new Scene();
        /* end of [g1.s2] */

        /* [g5.s3] */
        private int _cur_page = 0; //当前分机页数：从0开始；
        private int _Cur_Page
        {
            set
            {
                /* 页面按钮、显示逻辑 */
                int tempPages = _subDeviceCnt / (int)NUM_ONDIVPAGEPNL_SUBCNT.Value
                    + (_subDeviceCnt % (int)NUM_ONDIVPAGEPNL_SUBCNT.Value == 0 ? 0 : 1);
                if (value < 0 || value >= tempPages)
                    return;
                BTN_ONDIVPAGEPNL_PRE.Enabled = BTN_ONDIVPAGEPNL_POS.Enabled = true;
                if (value == 0)
                    BTN_ONDIVPAGEPNL_PRE.Enabled = false;
                if (value == tempPages - 1)
                    BTN_ONDIVPAGEPNL_POS.Enabled = false;
                if (tempPages == 1)
                    BTN_ONDIVPAGEPNL_POS.Enabled = BTN_ONDIVPAGEPNL_PRE.Enabled = false;
                LBL_ONDIVPAGEPNL_PAGE.Text = (value + 1).ToString() + "/" +
                    tempPages.ToString();
                _cur_page = value;
                
            }
            get
            {
                return _cur_page;
            }
        }


        private int _subDeviceCnt = 0; //分机数量

        /*
         * 用于高级设备下拉列表框中；
         * 保存上次选中的序号；
         * 当两次选中的序号没有发生变化时，不触发页面刷新业务；
         * 以便提高用户体验；
         */
        private int _curSelectIndex = -1; 

        /* end of [g5.s3] */
		public SceneForm()
		{
			InitializeComponent();

			// 场景类型
			IDictionary typeMap = new Dictionary<string, SceneType>();
			typeMap.Add("自动", SceneType.Auto);
			typeMap.Add("智能", SceneType.ZhiNeng);
			typeMap.Add("布防", SceneType.BuFang);
			typeMap.Add("撤防", SceneType.CheFang);
			typeMap.Add("全关", SceneType.QuanGuan);
			typeMap.Add("全开", SceneType.QuanKai);
			typeMap.Add("总关", SceneType.ZongGuan);
			typeMap.Add("总开", SceneType.ZongKai);
			typeMap.Add("回家", SceneType.HuiJia);
			typeMap.Add("离家", SceneType.LiJia);
			typeMap.Add("就寝", SceneType.JiuQin);
			typeMap.Add("起夜", SceneType.QiChuang);
			typeMap.Add("起床", SceneType.QiChuang);
			typeMap.Add("就餐", SceneType.JiuCan);
			typeMap.Add("凉爽", SceneType.LiangShuang);
			typeMap.Add("温暖", SceneType.WenNuan);
			typeMap.Add("电视", SceneType.DianShi);
			typeMap.Add("影院", SceneType.YingYuan);
			typeMap.Add("音乐", SceneType.YinYue);
			typeMap.Add("娱乐", SceneType.YueLe);
			typeMap.Add("阅读", SceneType.YueDu);
			typeMap.Add("会客", SceneType.HuiKe);
			typeMap.Add("生日", SceneType.ShengRi);
			typeMap.Add("宴会", SceneType.YanHui);
			typeMap.Add("用户", SceneType.YongHu);
			selType.bindData(typeMap);


		}


		// 绑定数据
		public override void refreshFormData(System.Collections.Generic.IDictionary<string, object> dataMap)
		{
			_cur_scene = (Scene)dataMap["scene"];
            txtName.Value = _cur_scene.name;
            txtLampAreaId.Value = _cur_scene.lampAreaId;
            txtLampSceneId.Value = _cur_scene.lampSceneId;

            /* [g4.s1] */

            selType.Value = _cur_scene.type;

            /* end of [g4.s1] */

			// 单控设备列表
            //if(_cur_scene.events.Count < 1)
            sceneDeviceList.init(_cur_scene);
            //else
                
            /* [g5.s4] 清理旧逻辑 */
			// 高级设备列表
            //divExDevice.Controls.Clear();
            //createAttrList(findExDevice(ExDeviceType.CentralAircon), _cur_scene);
            //createAttrList(findExDevice(ExDeviceType.CentralHeating), _cur_scene);
            //createAttrList(findExDevice(ExDeviceType.CentralNewWind), _cur_scene);
            //createAttrList(findExDevice(ExDeviceType.BGM), _cur_scene);
            /* end of [g5.s4] */

            /* [g5.s7]刷新页面 */
            subsPageInit();
            Device d = (Device)COMBOB_ONDIVPAGEPNL_EXDEV.SelectedValue;
            if (d != null)
            {
                _Cur_Page = 0;
                createAttrList(d, _cur_scene);
            }

            /* end of [g5.s7] */


		}

		private void createAttrList(Device exDev, Scene scene)
		{
			if (exDev != null)
			{
                /* [g5.s1] 取消旧逻辑 */
                //// 设备名称
                //DivPanel dp = new DivPanel();
                //dp.Padding = new Padding(0);
                //dp.Margin = new Padding(0);
                //dp.Height = 25;
				
                //Label ltlName = new Label();
                //ltlName.Text = exDev.nickName;

                //dp.Controls.Add(ltlName);
                //divExDevice.Controls.Add(dp);
                //// 分机列表
                //foreach (Device sub in exDev.childDevices)
                //{
                //    SceneAttrList attrList = new SceneAttrList();
                //    attrList.init(sub, scene);
                //    divExDevice.Controls.Add(attrList);
                //}
                /* end of [g5.s1] */
                /* [g5.s2] 根据每页单机数显示分机 */
                divExDevice.Controls.Clear(); //先清理控件
                _subDeviceCnt = exDev.childDevices.Count; //将分机数目填充到全局位置
                int temp_begin = _Cur_Page * (int)NUM_ONDIVPAGEPNL_SUBCNT.Value ;
                int temp_subs_left = _subDeviceCnt - temp_begin;
                int temp_end = temp_subs_left > (int)NUM_ONDIVPAGEPNL_SUBCNT.Value ?
                    temp_begin + (int)NUM_ONDIVPAGEPNL_SUBCNT.Value - 1 :
                    temp_begin + temp_subs_left - 1;

                
                for (int i = temp_begin; i <= temp_end; i++)
                {
                    SceneAttrList attrList = new SceneAttrList();
                    attrList.init(exDev.childDevices[i], scene);
                    divExDevice.Controls.Add(attrList);
                }
                /* end of [g5.s2] */

			}
		}

		// 数据修改事件
		public override void onFormChanged(BaseFormControl sender, System.EventArgs e)
		{
            /* [g1.s1] 将修改保存到内存 */

            /* 1.保存场景属性 */
            _cur_scene.name = (string)txtName.Value;
            if (!int.TryParse((string)txtLampAreaId.Value, out _cur_scene.lampAreaId))
                _cur_scene.lampAreaId = -1;
            if (!int.TryParse((string)txtLampSceneId.Value, out _cur_scene.lampSceneId))
                _cur_scene.lampSceneId = -1;
            _cur_scene.type = (SceneType)selType.Value;

            /* [g3.s1] */
            windowMain.doSaveScene(_cur_scene, null);
            /* end of [g3.s1] */

            /* [g1.s4] 优化场景保存 */


            /* 2. 清理掉所有事件*/
            //_cur_scene.events.Clear();
            /* 3.保存设备属性 */
            /* 由于已经保存值 */
            //foreach (Control c in divDevice.Controls)
            //{
            //    if (!(c is SceneDeviceList))
            //        continue;

            //}
            //_cur_scene.events = sceneDeviceList.TagScene.events;


            /* [g1.s3] 将设备的保存提升至窗口级别 */
            
            //foreach (Control c in divDevice.Controls)
            //{
            //    if (!(c is SceneDeviceList))
            //        continue;
            //    SceneDeviceList sdl = (SceneDeviceList)c;
            //    foreach (Control c1 in sdl.tblList.Controls)
            //    {
            //        if (!(c1 is SceneDeviceItem))
            //            continue;
            //        SceneDeviceItem sdi = (SceneDeviceItem)c1;
            //        sdi.SaveValue(_cur_scene);
            //    }
            //}

            /* end of [g1.s3] */

            /* end of [g1.s4] */

            /* [g2.s1] 将高级设备保存行为下降至SceneAttrList */
            ///* 3. 保存高级设备属性 */
            ///* [g1.s1] */
            //foreach (Control c in divExDevice.Controls)
            //{
            //    if (!(c is SceneAttrList))
            //        continue;
            //    SceneEvent se = (SceneEvent)((SceneAttrList)c).Value;
            //    /*  */
            //    if (null != se)
            //        _cur_scene.events.Add(se);
            //    if (null == se)
            //    {
            //        //GCERR:
            //        return;
            //    }
            //    //先删除
                

            //    /*  */
            //}
            ///* end of [g1.s1] */

            /* end of [g2.s2] */

		}


        /* [g5.s6] 相关方法 */


        /// <summary>
        /// 用在刷新ScenePage时使用
        /// </summary>
        private void subsPageInit()
        {
            /* clear all items */
            COMBOB_ONDIVPAGEPNL_EXDEV.DataSource = null;

            /* [g5.s8] 当删除高级设备后，界面优化 */
            divExDevice.Controls.Clear();
            //LBL_ONDIVPAGEPNL_PAGE.Text = "/";
            //BTN_ONDIVPAGEPNL_POS.Enabled = false;
            //BTN_ONDIVPAGEPNL_PRE.Enabled = false;
            //NUM_ONDIVPAGEPNL_SUBCNT.Enabled = false;
            PNL_ONDIVPAGEPNL_HOST.Visible = false;
            /* end of [g5.s8] */
            /* 1.fill ComboBox */
                /* 1.1 fill */
            List<SelectItem> tempItems = new List<SelectItem>();
            tempItems.Clear();
            foreach (DeviceGroup dg in proj.deviceGroups)
            {
                foreach (Device d in dg.devices)
                {
                    if (d.type.exType != ExDeviceType.NULL)
                    {
                        SelectItem tempSI = new SelectItem(d.nickName, d);
                        tempItems.Add(tempSI);
                    }
                }
            }
            COMBOB_ONDIVPAGEPNL_EXDEV.DisplayMember = "text";
            COMBOB_ONDIVPAGEPNL_EXDEV.ValueMember = "value";
            COMBOB_ONDIVPAGEPNL_EXDEV.DataSource = tempItems;
                /* 1.2 select */
            if (COMBOB_ONDIVPAGEPNL_EXDEV.Items.Count > 0)
            {
                COMBOB_ONDIVPAGEPNL_EXDEV.SelectedIndex = 0;
                /* 1.3 set _subDeviceCnt */
                _subDeviceCnt = ((Device)COMBOB_ONDIVPAGEPNL_EXDEV.SelectedValue).childDevices.Count;
                /* [g5.s9] */
                //NUM_ONDIVPAGEPNL_SUBCNT.Enabled = true;
                PNL_ONDIVPAGEPNL_HOST.Visible = true;

                /* end of [g5.s9] */
            }
            /* 2. initialise numeric combonent */
                /* 读取上次存储的配置 */
            NUM_ONDIVPAGEPNL_SUBCNT.Value = Properties.Settings.Default.ScenePnlSubsCnt;

            
           
        }

        /* end of [g5.s6] */

        /* [g5.s5]相关事件 */
        public void onPageBtn_Clicked(object sender, System.EventArgs e)
        {
            Device d = (Device)COMBOB_ONDIVPAGEPNL_EXDEV.SelectedValue;
            if (d == null)
                return;
            if (((Button)sender).Name == "BTN_ONDIVPAGEPNL_PRE")
            {//上一页
                _Cur_Page--;
            }
            else if (((Button)sender).Name == "BTN_ONDIVPAGEPNL_POS")
            {//下一页
                _Cur_Page++;
            }
            
            createAttrList(d, _cur_scene);
        }

        public void DropDown(object sender, System.EventArgs e)
        {
            _curSelectIndex = COMBOB_ONDIVPAGEPNL_EXDEV.SelectedIndex;
        }
        public void DropDownClosed(object sender, System.EventArgs e)
        {
            if (COMBOB_ONDIVPAGEPNL_EXDEV.SelectedIndex == _curSelectIndex)
                return; //与上次相同时，直接返回
            Device d = (Device)COMBOB_ONDIVPAGEPNL_EXDEV.SelectedValue;
            if (d == null)
                return;
            /**/
            _subDeviceCnt = d.childDevices.Count;
            _Cur_Page = 0;
            createAttrList(d, _cur_scene);
        }


        public void Numeric_ValueChanged(object sender, System.EventArgs e)
        {
            if (COMBOB_ONDIVPAGEPNL_EXDEV.SelectedValue == null)
                return;
            Device d = (Device)COMBOB_ONDIVPAGEPNL_EXDEV.SelectedValue;
            if (d == null)
                return;
            /* 刷新页数,姑且设为第0页 */
            _Cur_Page = 0;
            /* 刷新页面 */
            createAttrList(d, _cur_scene);
            /* 保存到软件配置 */
            Properties.Settings.Default.ScenePnlSubsCnt = (int)NUM_ONDIVPAGEPNL_SUBCNT.Value;

        }
        
        /* end of [g5.s5] */

	}
}
